package cn.itcast.crm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.crm.dao.ICustomerDao;
import cn.itcast.crm.domain.Customer;
import cn.itcast.crm.service.CustomerService;

@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
	@Autowired
	private ICustomerDao customerDao;
	
	@Override
	public List<Customer> findNoAssociationCustomers() {
		return customerDao.findByFixedAreaIdIsNull();
	}

	@Override
	public List<Customer> findHasAssociationFixedAreaCustomers(String fixedAreaid) {
		return customerDao.findByFixedAreaId(fixedAreaid);
	}

	@Override
	public void associationCustomersToFixedArea(String customerIdStr, String fixedAreid) {
		String[] customerIdArray = customerIdStr.split(",");
		for (String string : customerIdArray) {
			Integer id = Integer.parseInt(string);
			customerDao.updateFixedAreaId(fixedAreid, id);
		}
	}

	@Override
	public void regist(Customer customer) {
		customerDao.save(customer);
	}

	@Override
	public Customer findByTelephone(String telephone) {
		return customerDao.findByTelephone(telephone);
	}

	@Override
	public void updateType(String telephone) {
		customerDao.updateType(telephone);
	}

	@Override
	public Customer login(String telephone, String password) {
		return customerDao.findByTelephoneAndPassword(telephone, password);
	}

	@Override
	public String findFixedAreaIdByAddress(String address) {
		return customerDao.findFixedAreaIdByAddress(address);
	}

}
